import { Constant } from "../constant/Constant";
import NumberShow from "../number/NumberShow";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    private addScore: cc.Node = null;

    start() {
        this.addScore = cc.find("Canvas/Score/ScoLayer");
    }

    onCollisionEnter(other: cc.Component) {
        let otherNode = other.node;
        let t = this;
        if (otherNode.name == "Left" || otherNode.name == "Right") {
            Constant.NOW_SCORE += 3;
            t.addScore.getComponent(NumberShow).showNumber(Constant.NOW_SCORE);
            this.node.active = false;
            this.node.position = new cc.Vec2(0, 0);
        }
    }


    update(dt) {
        if (this.node.y < -Constant.SCREEN.height) {
            this.node.active = false;
            this.node.position = new cc.Vec2(0, 0);
        } else {
            let action = cc.moveBy(dt, new cc.Vec2(0, -Constant.GAME_SPEED));
            this.node.runAction(action);
        }
    }
}
